package com.clock.scheduler;


import com.sun.javafx.geom.AreaOp;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalTime;
import java.util.function.Consumer;

@Component
public class AlarmScheduler {

    private static final Logger log = LoggerFactory.getLogger(AlarmScheduler.class);

    private LocalTime alarmTime;
    private Consumer<LocalTime> callback;

    public void setAlarm(LocalTime time, Consumer<LocalTime> onAlarm) {
        this.alarmTime = time;
        this.callback = onAlarm;
    }

    @Scheduled(fixedRate = 1000)
    public void checkAlarm() {
        LocalTime now = LocalTime.now();
        if (alarmTime != null && now.getHour() == alarmTime.getHour() && now.getMinute() == alarmTime.getMinute()) {
            if (callback != null) {
                LocalTime tmpTime = alarmTime;
                callback.accept(tmpTime);
                alarmTime = null; // 防止多次触发
            }
        }
    }
}
